// CSIDLTESTER.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <InitGuid.h>
#include <KnownFolders.h>

std::wstring get_error_text(DWORD err) 
{ 
	// Retrieve the system error message for the last-error code

	LPVOID lpMsgBuf;

	FormatMessageW(
		FORMAT_MESSAGE_ALLOCATE_BUFFER | 
		FORMAT_MESSAGE_FROM_SYSTEM |
		FORMAT_MESSAGE_IGNORE_INSERTS,
		NULL,
		err,
		MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
		(LPTSTR) &lpMsgBuf,
		0, NULL );

	std::wstring strError((LPCWSTR)lpMsgBuf);

	LocalFree(lpMsgBuf);

	return strError;
}

void print_csidl(LPCWSTR szName,int csidl)
{
	WCHAR szPath[MAX_PATH + 1];
	memset(szPath,0,sizeof(szPath));

	if (::SHGetSpecialFolderPath(NULL, szPath, csidl, FALSE))
	{
		wprintf(L"%-30s",szName);
		wprintf(L" : ");
		wprintf(szPath);
		wprintf(L"\n");
	}
	else
	{	
		wprintf(L"%-30s",szName);
		wprintf(L" : ");
		wprintf(get_error_text(GetLastError()).c_str());
		//wprintf(L"\n");
	}
}

void print_folderid(LPCWSTR szName,REFKNOWNFOLDERID FOLDERID)
{
	PWSTR strpath = 0;
	HRESULT hr = SHGetKnownFolderPath(FOLDERID,0,0,&strpath);

	if (SUCCEEDED(hr))
	{		
		wprintf(L"%-30s",szName);
		wprintf(L" : ");
		wprintf(strpath);
		wprintf(L"\n");

		CoTaskMemFree(strpath);
	}
	else
	{
		wprintf(L"%-30s",szName);
		wprintf(L" : ");
		wprintf(get_error_text(hr).c_str());
		//wprintf(L"\n");
	}
}

#define PRINTFOLDERID(x) print_folderid(L#x,x)
#define PRINTCSIDL(x) print_csidl(L#x,x)

void printallcsidl()
{
	PRINTCSIDL(CSIDL_DESKTOP                   );//0x0000        // <desktop>
	PRINTCSIDL(CSIDL_INTERNET                  );//0x0001        // Internet Explorer (icon on desktop)
	PRINTCSIDL(CSIDL_PROGRAMS                  );//0x0002        // Start Menu\Programs
	PRINTCSIDL(CSIDL_CONTROLS                  );//0x0003        // My Computer\Control Panel
	PRINTCSIDL(CSIDL_PRINTERS                  );//0x0004        // My Computer\Printers
	PRINTCSIDL(CSIDL_PERSONAL                  );//0x0005        // My Documents
	PRINTCSIDL(CSIDL_FAVORITES                 );//0x0006        // <user name>\Favorites
	PRINTCSIDL(CSIDL_STARTUP                   );//0x0007        // Start Menu\Programs\Startup
	PRINTCSIDL(CSIDL_RECENT                    );//0x0008        // <user name>\Recent
	PRINTCSIDL(CSIDL_SENDTO                    );//0x0009        // <user name>\SendTo
	PRINTCSIDL(CSIDL_BITBUCKET                 );//0x000a        // <desktop>\Recycle Bin
	PRINTCSIDL(CSIDL_STARTMENU                 );//0x000b        // <user name>\Start Menu
	PRINTCSIDL(CSIDL_MYDOCUMENTS               );//CSIDL_PERSONAL //  Personal was just a silly name for My Documents
	PRINTCSIDL(CSIDL_MYMUSIC                   );//0x000d        // "My Music" folder
	PRINTCSIDL(CSIDL_MYVIDEO                   );//0x000e        // "My Videos" folder
	PRINTCSIDL(CSIDL_DESKTOPDIRECTORY          );//0x0010        // <user name>\Desktop
	PRINTCSIDL(CSIDL_DRIVES                    );//0x0011        // My Computer
	PRINTCSIDL(CSIDL_NETWORK                   );//0x0012        // Network Neighborhood (My Network Places)
	PRINTCSIDL(CSIDL_NETHOOD                   );//0x0013        // <user name>\nethood
	PRINTCSIDL(CSIDL_FONTS                     );//0x0014        // windows\fonts
	PRINTCSIDL(CSIDL_TEMPLATES                 );//0x0015
	PRINTCSIDL(CSIDL_COMMON_STARTMENU          );//0x0016        // All Users\Start Menu
	PRINTCSIDL(CSIDL_COMMON_PROGRAMS           );//0x0017        // All Users\Start Menu\Programs
	PRINTCSIDL(CSIDL_COMMON_STARTUP            );//0x0018        // All Users\Startup
	PRINTCSIDL(CSIDL_COMMON_DESKTOPDIRECTORY   );//0x0019        // All Users\Desktop
	PRINTCSIDL(CSIDL_APPDATA                   );//0x001a        // <user name>\Application Data
	PRINTCSIDL(CSIDL_PRINTHOOD                 );//0x001b        // <user name>\PrintHood

#ifndef CSIDL_LOCAL_APPDATA
	PRINTCSIDL(CSIDL_LOCAL_APPDATA             );//0x001c        // <user name>\Local Settings\Applicaiton Data (non roaming)
#endif // CSIDL_LOCAL_APPDATA

	PRINTCSIDL(CSIDL_ALTSTARTUP                );//0x001d        // non localized startup
	PRINTCSIDL(CSIDL_COMMON_ALTSTARTUP         );//0x001e        // non localized common startup
	PRINTCSIDL(CSIDL_COMMON_FAVORITES          );//0x001f

#ifndef _SHFOLDER_H_
	PRINTCSIDL(CSIDL_INTERNET_CACHE            );//0x0020
	PRINTCSIDL(CSIDL_COOKIES                   );//0x0021
	PRINTCSIDL(CSIDL_HISTORY                   );//0x0022
	PRINTCSIDL(CSIDL_COMMON_APPDATA            );//0x0023        // All Users\Application Data
	PRINTCSIDL(CSIDL_WINDOWS                   );//0x0024        // GetWindowsDirectory()
	PRINTCSIDL(CSIDL_SYSTEM                    );//0x0025        // GetSystemDirectory()
	PRINTCSIDL(CSIDL_PROGRAM_FILES             );//0x0026        // C:\Program Files
	PRINTCSIDL(CSIDL_MYPICTURES                );//0x0027        // C:\Program Files\My Pictures
#endif // _SHFOLDER_H_

	PRINTCSIDL(CSIDL_PROFILE                   );//0x0028        // USERPROFILE
	PRINTCSIDL(CSIDL_SYSTEMX86                 );//0x0029        // x86 system directory on RISC
	PRINTCSIDL(CSIDL_PROGRAM_FILESX86          );//0x002a        // x86 C:\Program Files on RISC

#ifndef _SHFOLDER_H_
	PRINTCSIDL(CSIDL_PROGRAM_FILES_COMMON      );//0x002b        // C:\Program Files\Common
#endif // _SHFOLDER_H_

	PRINTCSIDL(CSIDL_PROGRAM_FILES_COMMONX86   );//0x002c        // x86 Program Files\Common on RISC
	PRINTCSIDL(CSIDL_COMMON_TEMPLATES          );//0x002d        // All Users\Templates

#ifndef _SHFOLDER_H_
	PRINTCSIDL(CSIDL_COMMON_DOCUMENTS          );//0x002e        // All Users\Documents
	PRINTCSIDL(CSIDL_COMMON_ADMINTOOLS         );//0x002f        // All Users\Start Menu\Programs\Administrative Tools
	PRINTCSIDL(CSIDL_ADMINTOOLS                );//0x0030        // <user name>\Start Menu\Programs\Administrative Tools
#endif // _SHFOLDER_H_

	PRINTCSIDL(CSIDL_CONNECTIONS               );//0x0031        // Network and Dial-up Connections
	PRINTCSIDL(CSIDL_COMMON_MUSIC              );//0x0035        // All Users\My Music
	PRINTCSIDL(CSIDL_COMMON_PICTURES           );//0x0036        // All Users\My Pictures
	PRINTCSIDL(CSIDL_COMMON_VIDEO              );//0x0037        // All Users\My Video
	PRINTCSIDL(CSIDL_RESOURCES                 );//0x0038        // Resource Direcotry

#ifndef _SHFOLDER_H_
	PRINTCSIDL(CSIDL_RESOURCES_LOCALIZED       );//0x0039        // Localized Resource Direcotry
#endif // _SHFOLDER_H_

	PRINTCSIDL(CSIDL_COMMON_OEM_LINKS          );//0x003a        // Links to All Users OEM specific apps
	PRINTCSIDL(CSIDL_CDBURN_AREA               );//0x003b        // USERPROFILE\Local Settings\Application Data\Microsoft\CD Burning
	// unused                               );//0x003c
	PRINTCSIDL(CSIDL_COMPUTERSNEARME           );//0x003d        // Computers Near Me (computered from Workgroup membership)

#ifndef _SHFOLDER_H_
	PRINTCSIDL(CSIDL_FLAG_CREATE               );//0x8000        // combine with CSIDL_ value to force folder creation in SHGetFolderPath()
#endif // _SHFOLDER_H_

	PRINTCSIDL(CSIDL_FLAG_DONT_VERIFY          );//0x4000        // combine with CSIDL_ value to return an unverified folder path
	PRINTCSIDL(CSIDL_FLAG_DONT_UNEXPAND        );//0x2000        // combine with CSIDL_ value to avoid unexpanding environment variables

#if (NTDDI_VERSION >= NTDDI_WINXP)
	PRINTCSIDL(CSIDL_FLAG_NO_ALIAS             );//0x1000        // combine with CSIDL_ value to insure non-alias versions of the pidl
	PRINTCSIDL(CSIDL_FLAG_PER_USER_INIT        );//0x0800        // combine with CSIDL_ value to indicate per-user init (eg. upgrade)
#endif  // NTDDI_WINXP

	PRINTCSIDL(CSIDL_FLAG_MASK                 );//0xFF00        // mask for all possible flag values
}

void print_all_folderid()
{
	PRINTFOLDERID(FOLDERID_NetworkFolder);

	// {0AC0837C-BBF8-452A-850D-79D08E667CA7}
	PRINTFOLDERID(FOLDERID_ComputerFolder);

	// {4D9F7874-4E0C-4904-967B-40B0D20C3E4B}
	PRINTFOLDERID(FOLDERID_InternetFolder);

	// {82A74AEB-AEB4-465C-A014-D097EE346D63}
	PRINTFOLDERID(FOLDERID_ControlPanelFolder);

	// {76FC4E2D-D6AD-4519-A663-37BD56068185}
	PRINTFOLDERID(FOLDERID_PrintersFolder);

	// {43668BF8-C14E-49B2-97C9-747784D784B7}
	PRINTFOLDERID(FOLDERID_SyncManagerFolder);

	// {0F214138-B1D3-4a90-BBA9-27CBC0C5389A}
	PRINTFOLDERID(FOLDERID_SyncSetupFolder);

	// {4bfefb45-347d-4006-a5be-ac0cb0567192}
	PRINTFOLDERID(FOLDERID_ConflictFolder);

	// {289a9a43-be44-4057-a41b-587a76d7e7f9}
	PRINTFOLDERID(FOLDERID_SyncResultsFolder);

	// {B7534046-3ECB-4C18-BE4E-64CD4CB7D6AC}
	PRINTFOLDERID(FOLDERID_RecycleBinFolder);

	// {6F0CD92B-2E97-45D1-88FF-B0D186B8DEDD}
	PRINTFOLDERID(FOLDERID_ConnectionsFolder);

	// {FD228CB7-AE11-4AE3-864C-16F3910AB8FE}
	PRINTFOLDERID(FOLDERID_Fonts);

	// display name:        "Desktop"
	// default path:        "C:\Users\<UserName>\Desktop"
	// legacy default path: "C:\Documents and Settings\<userName>\Desktop"
	// legacy CSIDL value:  CSIDL_DESKTOP
	// {B4BFCC3A-DB2C-424C-B029-7FE99A87C641}
	PRINTFOLDERID(FOLDERID_Desktop);

	// {B97D20BB-F46A-4C97-BA10-5E3608430854}
	PRINTFOLDERID(FOLDERID_Startup);

	// {A77F5D77-2E2B-44C3-A6A2-ABA601054A51}
	PRINTFOLDERID(FOLDERID_Programs);

	// {625B53C3-AB48-4EC1-BA1F-A1EF4146FC19}
	PRINTFOLDERID(FOLDERID_StartMenu);

	// {AE50C081-EBD2-438A-8655-8A092E34987A}
	PRINTFOLDERID(FOLDERID_Recent);

	// {8983036C-27C0-404B-8F08-102D10DCFD74}
	PRINTFOLDERID(FOLDERID_SendTo);

	// {FDD39AD0-238F-46AF-ADB4-6C85480369C7}
	PRINTFOLDERID(FOLDERID_Documents);

	// {1777F761-68AD-4D8A-87BD-30B759FA33DD}
	PRINTFOLDERID(FOLDERID_Favorites);

	// {C5ABBF53-E17F-4121-8900-86626FC2C973}
	PRINTFOLDERID(FOLDERID_NetHood);

	// {9274BD8D-CFD1-41C3-B35E-B13F55A758F4}
	PRINTFOLDERID(FOLDERID_PrintHood);

	// {A63293E8-664E-48DB-A079-DF759E0509F7}
	PRINTFOLDERID(FOLDERID_Templates);

	// {82A5EA35-D9CD-47C5-9629-E15D2F714E6E}
	PRINTFOLDERID(FOLDERID_CommonStartup);

	// {0139D44E-6AFE-49F2-8690-3DAFCAE6FFB8}
	PRINTFOLDERID(FOLDERID_CommonPrograms);

	// {A4115719-D62E-491D-AA7C-E74B8BE3B067}
	PRINTFOLDERID(FOLDERID_CommonStartMenu);

	// {C4AA340D-F20F-4863-AFEF-F87EF2E6BA25}
	PRINTFOLDERID(FOLDERID_PublicDesktop);

	// {62AB5D82-FDC1-4DC3-A9DD-070D1D495D97}
	PRINTFOLDERID(FOLDERID_ProgramData);

	// {B94237E7-57AC-4347-9151-B08C6C32D1F7}
	PRINTFOLDERID(FOLDERID_CommonTemplates);

	// {ED4824AF-DCE4-45A8-81E2-FC7965083634}
	PRINTFOLDERID(FOLDERID_PublicDocuments);

	// {3EB685DB-65F9-4CF6-A03A-E3EF65729F3D}
	PRINTFOLDERID(FOLDERID_RoamingAppData);

	// {F1B32785-6FBA-4FCF-9D55-7B8E7F157091}
	PRINTFOLDERID(FOLDERID_LocalAppData);

	// {A520A1A4-1780-4FF6-BD18-167343C5AF16}
	PRINTFOLDERID(FOLDERID_LocalAppDataLow);

	// {352481E8-33BE-4251-BA85-6007CAEDCF9D}
	PRINTFOLDERID(FOLDERID_InternetCache);

	// {2B0F765D-C0E9-4171-908E-08A611B84FF6}
	PRINTFOLDERID(FOLDERID_Cookies);

	// {D9DC8A3B-B784-432E-A781-5A1130A75963}
	PRINTFOLDERID(FOLDERID_History);

	// {1AC14E77-02E7-4E5D-B744-2EB1AE5198B7}
	PRINTFOLDERID(FOLDERID_System);

	// {D65231B0-B2F1-4857-A4CE-A8E7C6EA7D27}
	PRINTFOLDERID(FOLDERID_SystemX86);

	// {F38BF404-1D43-42F2-9305-67DE0B28FC23}
	PRINTFOLDERID(FOLDERID_Windows);

	// {5E6C858F-0E22-4760-9AFE-EA3317B67173}
	PRINTFOLDERID(FOLDERID_Profile);

	// {33E28130-4E1E-4676-835A-98395C3BC3BB}
	PRINTFOLDERID(FOLDERID_Pictures);

	// {7C5A40EF-A0FB-4BFC-874A-C0F2E0B9FA8E}
	PRINTFOLDERID(FOLDERID_ProgramFilesX86);

	// {DE974D24-D9C6-4D3E-BF91-F4455120B917}
	PRINTFOLDERID(FOLDERID_ProgramFilesCommonX86);

	// {6D809377-6AF0-444b-8957-A3773F02200E}
	PRINTFOLDERID(FOLDERID_ProgramFilesX64);

	// {6365D5A7-0F0D-45e5-87F6-0DA56B6A4F7D}
	PRINTFOLDERID(FOLDERID_ProgramFilesCommonX64);

	// {905e63b6-c1bf-494e-b29c-65b732d3d21a}
	PRINTFOLDERID(FOLDERID_ProgramFiles);

	// {F7F1ED05-9F6D-47A2-AAAE-29D317C6F066}
	PRINTFOLDERID(FOLDERID_ProgramFilesCommon);

	// {724EF170-A42D-4FEF-9F26-B60E846FBA4F}
	PRINTFOLDERID(FOLDERID_AdminTools);

	// {D0384E7D-BAC3-4797-8F14-CBA229B392B5}
	PRINTFOLDERID(FOLDERID_CommonAdminTools);

	// {4BD8D571-6D19-48D3-BE97-422220080E43}
	PRINTFOLDERID(FOLDERID_Music);

	// {18989B1D-99B5-455B-841C-AB7C74E4DDFC}
	PRINTFOLDERID(FOLDERID_Videos);

	// {B6EBFB86-6907-413C-9AF7-4FC2ABF07CC5}
	PRINTFOLDERID(FOLDERID_PublicPictures);

	// {3214FAB5-9757-4298-BB61-92A9DEAA44FF}
	PRINTFOLDERID(FOLDERID_PublicMusic);

	// {2400183A-6185-49FB-A2D8-4A392A602BA3}
	PRINTFOLDERID(FOLDERID_PublicVideos);

	// {8AD10C31-2ADB-4296-A8F7-E4701232C972}
	PRINTFOLDERID(FOLDERID_ResourceDir);

	// {2A00375E-224C-49DE-B8D1-440DF7EF3DDC}
	PRINTFOLDERID(FOLDERID_LocalizedResourcesDir);

	// {C1BAE2D0-10DF-4334-BEDD-7AA20B227A9D}
	PRINTFOLDERID(FOLDERID_CommonOEMLinks);

	// {9E52AB10-F80D-49DF-ACB8-4330F5687855}
	PRINTFOLDERID(FOLDERID_CDBurning);

	// {0762D272-C50A-4BB0-A382-697DCD729B80}
	PRINTFOLDERID(FOLDERID_UserProfiles);

	// {DE92C1C7-837F-4F69-A3BB-86E631204A23}
	PRINTFOLDERID(FOLDERID_Playlists);

	// {15CA69B3-30EE-49C1-ACE1-6B5EC372AFB5}
	PRINTFOLDERID(FOLDERID_SamplePlaylists);

	// {B250C668-F57D-4EE1-A63C-290EE7D1AA1F}
	PRINTFOLDERID(FOLDERID_SampleMusic);

	// {C4900540-2379-4C75-844B-64E6FAF8716B}
	PRINTFOLDERID(FOLDERID_SamplePictures);

	// {859EAD94-2E85-48AD-A71A-0969CB56A6CD}
	PRINTFOLDERID(FOLDERID_SampleVideos);

	// {69D2CF90-FC33-4FB7-9A0C-EBB0F0FCB43C}
	PRINTFOLDERID(FOLDERID_PhotoAlbums);

	// {DFDF76A2-C82A-4D63-906A-5644AC457385}
	PRINTFOLDERID(FOLDERID_Public);

	// {df7266ac-9274-4867-8d55-3bd661de872d}
	PRINTFOLDERID(FOLDERID_ChangeRemovePrograms);

	// {a305ce99-f527-492b-8b1a-7e76fa98d6e4}
	PRINTFOLDERID(FOLDERID_AppUpdates);

	// {de61d971-5ebc-4f02-a3a9-6c82895e5c04}
	PRINTFOLDERID(FOLDERID_AddNewPrograms);

	// {374DE290-123F-4565-9164-39C4925E467B}
	PRINTFOLDERID(FOLDERID_Downloads);

	// {3D644C9B-1FB8-4f30-9B45-F670235F79C0}
	PRINTFOLDERID(FOLDERID_PublicDownloads);

	// {7d1d3a04-debb-4115-95cf-2f29da2920da}
	PRINTFOLDERID(FOLDERID_SavedSearches);

	// {52a4f021-7b75-48a9-9f6b-4b87a210bc8f}
	PRINTFOLDERID(FOLDERID_QuickLaunch);

	// {56784854-C6CB-462b-8169-88E350ACB882}
	PRINTFOLDERID(FOLDERID_Contacts);

	// {A75D362E-50FC-4fb7-AC2C-A8BEAA314493}
	PRINTFOLDERID(FOLDERID_SidebarParts);

	// {7B396E54-9EC5-4300-BE0A-2482EBAE1A26}
	PRINTFOLDERID(FOLDERID_SidebarDefaultParts);

	// {5b3749ad-b49f-49c1-83eb-15370fbd4882}
	PRINTFOLDERID(FOLDERID_TreeProperties);

	// {DEBF2536-E1A8-4c59-B6A2-414586476AEA}
	PRINTFOLDERID(FOLDERID_PublicGameTasks);

	// {054FAE61-4DD8-4787-80B6-090220C4B700}
	PRINTFOLDERID(FOLDERID_GameTasks);

	// {4C5C32FF-BB9D-43b0-B5B4-2D72E54EAAA4}
	PRINTFOLDERID(FOLDERID_SavedGames);

	// {CAC52C1A-B53D-4edc-92D7-6B2E8AC19434}
	PRINTFOLDERID(FOLDERID_Games);

	// {bd85e001-112e-431e-983b-7b15ac09fff1}
	PRINTFOLDERID(FOLDERID_RecordedTV);

	// {98ec0e18-2098-4d44-8644-66979315a281}
	PRINTFOLDERID(FOLDERID_SEARCH_MAPI);

	// {ee32e446-31ca-4aba-814f-a5ebd2fd6d5e}
	PRINTFOLDERID(FOLDERID_SEARCH_CSC);

	// {bfb9d5e0-c6a9-404c-b2b2-ae6db6af4968}
	PRINTFOLDERID(FOLDERID_Links);

	// {f3ce0f7c-4901-4acc-8648-d5d44b04ef8f}
	PRINTFOLDERID(FOLDERID_UsersFiles);

	// {190337d1-b8ca-4121-a639-6d472d16972a}
	PRINTFOLDERID(FOLDERID_SearchHome);

	// {2C36C0AA-5812-4b87-BFD0-4CD0DFB19B39}
	PRINTFOLDERID(FOLDERID_OriginalImages);
}

int _tmain(int argc, _TCHAR* argv[])
{

	setlocale(LC_ALL,"chs");

	wprintf(L"\n_____________________________________________________CSIDL________________________________________________________\n\n");
	printallcsidl();
	wprintf(L"\n____________________________________________________FOLDERID______________________________________________________\n\n");

	print_all_folderid();
	getchar();
	return 0;
}

